package dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pojo.User;
import util.DBUtil;
import dao.UserDao;

public class UserDaoImpl implements UserDao{

	@Override
	public void addUser(User user) throws SQLException{
		String sql = "insert into users (uname,sal,birth,sex,portrait) values(?,?,?,?,?)";
		Connection conn=null;
		PreparedStatement pst = null;
		conn=DBUtil.getConn();
		pst = conn.prepareStatement(sql);
		pst.setString(1, user.getName());
		pst.setDouble(2, user.getSal());
		pst.setString(3, user.getBirth());
		pst.setBoolean(4, user.isSex());
		pst.setBytes(5, user.getPortait());
		pst.executeUpdate();
		DBUtil.close(conn, pst, null);
	
	
		
	}

	@Override
	public List<User> getAll() {
		List<User> uList=new ArrayList<User>();
		ResultSet rs=null;
		String sql = "select * from users";
		Connection conn = DBUtil.getConnectionWithDs();
		PreparedStatement pst = null;
		try {
			pst=conn.prepareStatement(sql);
			rs=pst.executeQuery();
			while(rs.next()) {
				User user=new User();
				user.setName(rs.getString("uname"));
				user.setSal(rs.getDouble("sal"));
				user.setBirth(rs.getString("birth"));
				user.setSex(rs.getBoolean("sex"));
				user.setPortait(rs.getBytes("portrait"));
				uList.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return uList;
	}
	

}
